Welcome to Deploying Web Apps Using Flask. After watching this video, you will be able to Describe Flask features Install Flask Create and deploy a Python web app using Flask Flask is a micro-framework for creating web applications quickly and easily with Python. Flask supports CRUD, which is nothing but create, read, update, and delete requests by making POST, PUT, GET, PATCH, and DELETE requests. Here is the basic structure of a Flask application. Notice the logo of the Flask package. Let's look at how you can use Flask for CRUD operations. POST requests are used for creating objects or data. For example, you can use POST requests to create a user. In most web applications, you will see POST being used for creating objects or data. You can use the GET request to read data from the server. You can use PUT and PATCH requests to update the existing data or objects. You can use DELETE requests to delete the existing data or objects. Note, most web applications tend to use POST for creating, updating, and deleting objects and data, and GET for reading. Another video explains the POST, PUT, and GET requests in detail. Next, let's look at how to create a web application with Flask. The first step is to install the Flask package using pip, the standard package manager for Python. Like all other packages, you can use the command pip install flask, which will get the most current version of Flask. The Flask package is installed, and you are all set to create the web application. Next, import the Flask package, instantiate Flask class, create a web application, and then run the application. For the purposes of demonstration, let's look at the web application that returns hello world string as a response to a GET request. Step 1. Install Flask. Step 2. Import the Flask module from the Flask package. To do this, import the Flask class from the flask module. Step 3. Create an object of the Flask class as a web application with the name MyFirstWebApplication and store it as app. For example, app equal to Flask with MyFirstApplication. Most applications use the reference name app for clarity purposes. However, app is just a reference name, and you can use any other name. Step 4. Define the root and the method that will be invoked when this root is accessed. For example, enter at myapp.root open round bracket single quote slash single quote close round bracket. Notice in this example, neither GET nor POST have been specified. When you do not specify a request, by default, GET is requested. Therefore, in the example, the endpoint will now be able to serve GET requests to the root. Step 5. Hello is the method, written as def hello open round bracket close round bracket colon. The hello world method will be invoked when the system accesses the API endpoint that was defined in the previous step. The code to do this is app.run open round bracket debug. It takes no parameters and returns the string hello world. In the code, this is written as return single quote hello world exclamation sign quote. Step 6. Add a condition that web application should run only if name attribute is set to main. By default, name is set to main unless it is explicitly changed. You can save and run this like any other Python application. App myapp.run open round bracket debug equals true with capital T close round bracket. To start the server in a development environment, you need to save the code in a Python file and run it like any other Python application. When the web application server starts, it gives you the IP address and the port the application can be accessed from. To check the endpoints, you can open the browser and connect to this endpoint you see in the server output 127.0.0.1 at port 5000 and see the string hello world being returned from the web server application. Templates are nothing but pre-created HTML pages that are served from the web application. They can be both static and dynamic. By default, the Flask application looks for the templates in a directory called templates under the root directory. If the templates need to use images, styling sheets, or JavaScript files, which are stored in a folder named static in the root directory, the static pages are rendered as is. The dynamic pages usually have information that is filled in dynamically for each request. These pages are often based on the values passed as arguments. The arguments can either be passed through the URL or be passed as request parameters. Let's look at a sample Flask application. We start with importing Flask to create web application, request to handle the incoming request, and render underscore template for rendering static and dynamic HTML pages. Type from Flask F lowercase, import Flask with capital F comma, render underscore template comma request. Next, instantiate Flask and set the static folder. For example, myapp equals Flask open round bracket double quote my with capital M. First, application with capital A. The default folder name is static, but you can have the static contents in a directory with a different name as long as you set it explicitly. Notice there are three endpoints in this web application. The first is slash sample. This will render a static HTML page. The image in this HTML is sourced from the static directory. The code to accomplish this is myapp dot root open round bracket single quote slash sample single quote close round bracket. In the second line, type def get sample HTML. Note there is no space between get sample HTML. In the third line, type return render template sample dot HTML. The next one is slash user slash username, where the username that you see within angle brackets is the parameter. The code is written as at myapp dot root. Users specify the username methods equals get. If you see the method has been explicitly set to get, this is just to show how you can set the type of request. If nothing is specified, it is considered a get request. The page is rendered with the parameter we passed in the URL. The last one is slash user slash username, where the username is passed as a request parameter. The page is rendered with the parameter which was passed as a request. In this video, you learned that Flask is a micro framework for creating web applications and supports CRUD. Install the Flask package using pip. To create a web application using Flask, import Flask, instantiate Flask, run the app. You can render both static and dynamic templates with Flask.